package com.xtkj.superclub_friendscircle.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xtkj.superclub_entity.entity.friendsCircle.DTO.LikedCountDTO;
import com.xtkj.superclub_entity.entity.friendsCircle.FriendCicleShare;
import com.xtkj.superclub_entity.entity.friendsCircle.UserLike;
import com.xtkj.superclub_friendscircle.dao.IFriendCicleShareDao;
import com.xtkj.superclub_friendscircle.dao.IUserLikeDao;
import com.xtkj.superclub_friendscircle.service.ILike2DBService;
import com.xtkj.superclub_friendscircle.service.ILikes2RedisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 项目名称：superclub
 * 类 名 称：LikedServiceImpl
 * 类 描 述：TODO
 * 创建时间：2020/1/3 13:49
 * 创 建 人：xiezhiyan
 */
@Service
public class Like2DBServiceImpl implements ILike2DBService {
    @Autowired
    IUserLikeDao userLikeDao;

    @Autowired
    ILikes2RedisService redisService;

    @Autowired
    IFriendCicleShareDao friendCicleShareDao;

    @Override
    @Transactional
    public void  save(UserLike userLike) {
        userLikeDao.insert(userLike);
    }

    @Override
    @Transactional
    public void saveAll(List<UserLike> list) {
        for (UserLike userLike : list) {
            userLikeDao.insert(userLike);
        }
    }

    @Override
    public List<UserLike> getLikedListByShareId(String shareId, Pageable pageable){
        //根据被点赞分享的id查询点赞列表（即查询都谁给这个分享点赞过）
        return userLikeDao.selectList(new QueryWrapper<UserLike>().eq("share_id", shareId));
    }

    @Override
    public List<UserLike> getLikedListByLikesUserId(String likesUserId, Pageable pageable){
       // 根据点赞人的id查询点赞列表（即查询这个人都给哪些文章点赞过）
        return userLikeDao.selectList(new QueryWrapper<UserLike>().eq("likes_user_id", likesUserId));
    }

    @Override
    public UserLike getByLikedUserIdAndShareId(String likedUserId, String shareId) {
        //通过被点赞分享和点赞人id查询是否存在点赞记录
        return userLikeDao.selectOne(new QueryWrapper<UserLike>().eq("share_id", shareId).eq("likes_user_id", likedUserId));
    }

    @Override
    @Transactional
    public void transLikedFromRedis2DB() {
        List<UserLike> list = redisService.getLikedDataFromRedis();
        for (UserLike like : list) {
            UserLike ul = getByLikedUserIdAndShareId(like.getLikesUserId(), like.getShareId());
            if (ul == null){
                //没有记录，直接存入
                save(like);
            }else{
                //有记录，需要更新
                ul.setStatus(like.getStatus());
                save(ul);
            }
        }
    }

    @Override
    @Transactional
    public void transLikedCountFromRedis2DB() {
        List<LikedCountDTO> list = redisService.getLikedCountFromRedis();
        for (LikedCountDTO dto : list) {
            FriendCicleShare friendCicleShare = friendCicleShareDao.selectById(dto.getShareId());
            //点赞数量属于无关紧要的操作，出错无需抛异常
            if (friendCicleShare != null){
                Integer likeNum = friendCicleShare.getLikeNum() + dto.getCount();
                friendCicleShare.setLikeNum(likeNum);
                //更新点赞数量
                friendCicleShareDao.updateById(friendCicleShare);
            }
        }
    }
}